<?php

# MantisBT - a php based bugtracking system

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT.  If not, see <http://www.gnu.org/licenses/>.

	/**
	 * @package MantisBT
	 * @copyright Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
	 * @copyright Copyright (C) 2002 - 2010  MantisBT Team - mantisbt-dev@lists.sourceforge.net
	 * @link http://www.mantisbt.org
	 */
	 /**
	  * MantisBT Core API's
	  */
	require_once( 'core.php' );

	require_once( 'summary_api.php' );

	$f_project_id = gpc_get_int( 'project_id', helper_get_current_project() );

	# Override the current page to make sure we get the appropriate project-specific configuration
	$g_project_override = $f_project_id;

	access_ensure_project_level( config_get( 'view_summary_threshold' ) );

	$t_user_id = auth_get_current_user_id();

	$t_project_ids = user_get_all_accessible_projects( $t_user_id, $f_project_id);
	$specific_where = helper_project_specific_where( $f_project_id, $t_user_id);

	$t_bug_table = db_get_table( 'mantis_bug_table' );
	$t_history_table = db_get_table( 'mantis_bug_history_table' );

	$t_resolved = config_get( 'bug_resolved_status_threshold' );
	# the issue may have passed through the status we consider resolved
	#  (e.g., bug is CLOSED, not RESOLVED). The linkage to the history field
	#  will look up the most recent 'resolved' status change and return it as well
	$query = "SELECT b.id, b.date_submitted, b.last_updated, MAX(h.date_modified) as hist_update, b.status
        FROM $t_bug_table b LEFT JOIN $t_history_table h
            ON b.id = h.bug_id  AND h.type=0 AND h.field_name='status' AND h.new_value=" . db_param() . "
            WHERE b.status >=" . db_param() . " AND $specific_where
            GROUP BY b.id, b.status, b.date_submitted, b.last_updated
            ORDER BY b.id ASC";
	$result = db_query_bound( $query, Array( $t_resolved, $t_resolved ) );
	$bug_count = db_num_rows( $result );

	$t_bug_id       = 0;
	$t_largest_diff = 0;
	$t_total_time   = 0;
	for ($i=0;$i<$bug_count;$i++) {
		$row = db_fetch_array( $result );
		$t_date_submitted = $row['date_submitted'];
		$t_id = $row['id'];
		$t_status = $row['status'];
		if ( $row['hist_update'] !== NULL ) {
            $t_last_updated   = $row['hist_update'];
        } else {
        	$t_last_updated   = $row['last_updated'];
        }

		if ($t_last_updated < $t_date_submitted) {
			$t_last_updated   = 0;
			$t_date_submitted = 0;
		}

		$t_diff = $t_last_updated - $t_date_submitted;
		$t_total_time = $t_total_time + $t_diff;
		if ( $t_diff > $t_largest_diff ) {
			$t_largest_diff = $t_diff;
			$t_bug_id = $row['id'];
		}
	}
	if ( $bug_count < 1 ) {
		$bug_count = 1;
	}
	$t_average_time 	= $t_total_time / $bug_count;

	$t_largest_diff 	= number_format( $t_largest_diff / SECONDS_PER_DAY, 2 );
	$t_total_time		= number_format( $t_total_time / SECONDS_PER_DAY, 2 );
	$t_average_time 	= number_format( $t_average_time / SECONDS_PER_DAY, 2 );

	$t_orct_arr = preg_split( '/[\)\/\(]/', lang_get( 'orct' ), -1, PREG_SPLIT_NO_EMPTY );

	$t_orcttab = "";
	foreach ( $t_orct_arr as $t_orct_s ) {
		$t_orcttab .= '<td class="right">';
		$t_orcttab .= $t_orct_s;
		$t_orcttab .= '</td>';
	}

	html_page_top( lang_get( 'summary_link' ) );
?>
<br />
<?php
$t_current_user_name = current_user_get_field( 'username' );

//Function Counting time query
function Query_time ($displ = false) 
{
	$qt = 0;
	$qn = 0;
	$sql_time = QueryResultsAsArray("SHOW profiles");
	foreach ($sql_time as $o) {
	$qt = number_format($o->Duration + $qt,6,'.',' ');
	$qn++;
	}
	if ($displ == false)
	return $qt;
	else echo "$qn Querys&nbsp;in&nbsp;".$qt."&nbsp;sec.";
	return ;
}
$starttime = microtime();
$endtime = microtime();
$do = $_GET['do'];
$time = $_GET['time'];
$order = $_GET['order'];

//List danh sach theo request mysql
$qrOrder = mysql_query("select spcode, id, phone, status, city, from_unixtime(start_time), confirm from mantis_care_report_table where status='$order' and from_unixtime(start_time, '%c')='$time' order by from_unixtime(start_time) DESC");
$qrOrderTotal = mysql_query("select count(id) from mantis_care_report_table where status='$order' and from_unixtime(start_time, '%c')='$time' order by from_unixtime(start_time) DESC");
$qrOrderCity  = mysql_query("select count(city), city from mantis_care_report_table where status='virus' and from_unixtime(start_time, '%c')=2
 group by city order by from_unixtime(start_time) DESC");
	echo "DATA CENTER REQUEST<br><br>
	$ Show list [".$_GET['order']."]<br>
	Total: ";
	while ($row = mysql_fetch_array($qrOrderTotal))
	{echo $row['count(id)']."<br>=================================================<br><br>";}
		
	echo "<table border=1 cellpadding=0 cellspacing=0 style='border-collapse:collapse'>";
	echo "<tr><td>ID</td><td><a href=view_care_report.php?do=view&id=$id>UID</a></td><td>SUPPORT CODE</td><td>STATUS</td><td>CITY</td><td>TIME</td><td>PHAN HOI</td></tr>";
	$run = 0;
	while ($row = mysql_fetch_array($qrOrder))
	{
		$run++;
		$id = $row['id']; 
		$status = $row['status']; 
		$spcode = $row['spcode'];
		$city = $row['city']; 
		$qrTime = $row['from_unixtime(start_time)']; 
		$confirm = $row['confirm']; 
		echo "<tr><td>$run</td><td><a href=view_care_report.php?do=view&id=$id>".$id."</a></td><td>".$spcode."</td><td>".$status."</td><td>".$city."</td><td>$qrTime</td><td>$confirm</td></tr>";
	}	
	echo "</table>";
?>
<br>
<?php
	html_page_bottom();
